<template>
  <div class="biz-container">
    <table-layout :haveSider="false">
      <template #searchBox>
        <search-layout
          :formInfos="q_formInfos"
          :formDatas="q_formDatas"
          message="提示 : 添加商品对照前请先在商品管理中添加商品！"
          @onFind="onFind"
        ></search-layout>
      </template>
      <template #batchOperat>
        <batch-operat-btn 
          :addData="true" @onAddData="onAddData"
          :optData="optText" @onOptData="onBatchAudit">
          <template #cusBtn>
            <!-- <a-button type="primary" icon="vertical-align-bottom" size="small"> 自定义按钮 </a-button> -->
            <!-- <im-export></im-export> -->
          </template>
        </batch-operat-btn>
      </template>
      <template #tableList="slotData">
        <a-list-table
          :columns="columns"
          :tableData="listdata"
          :pagination="pagination"
          :isSelecttion="true"
          :loading="loading"
          :scroll="{y:slotData.tableHeight}"
          @onTableChange="handleTableChange"
          @onselectChange="onselectChange"
          @onAction="onAction"
          @onTouchCell="onTouchCell"
        >
        </a-list-table>
      </template>
    </table-layout>
     <customerSaleGoodsModal ref="modal" @ok="handleModalSaveOk" @close="handleModalCancel" />
     <viewModal ref="viewModal" @ok="handleModalSaveOk" @close="handleModalCancel" />
  </div>
</template>

<script>
import searchLayout from '@/views/app/biz/common/searchLayout'
import batchOperatBtn from '@/views/app/biz/common/batchOperatBtn'
import tableLayout from '@/views/app/biz/common/index'
import aListInfo from '@/views/app/biz/common/aListInfo'
import aListTable from '@/views/app/biz/common/aListTable'
import customerSaleGoodsModal from './customerSaleGoodsModal'
import viewModal from './viewModal'
import {
  getByConditions,
  getByConditionsCount,
  pushInfo
} from './customersalegoods'


// 字段列表
const columns = [
  { title: '销售代码', dataIndex: 'chanPingdm', key: 'chanPingdm', align: 'center' },
  { title: '客户名称', dataIndex: 'keHumc', key: 'keHumc', align: 'center' },
  { title: '品名', dataIndex: 'pinMing', key: 'pinMing', align: 'center',scopedSlots: { customRender: 'column' }},
  { title: '通用名', dataIndex: 'tongYongm', key: 'tongYongm', align: 'center' },
  { title: '注册证号', dataIndex: 'zhuCezh', key: 'zhuCezh', align: 'center' },
  { title: '规格', dataIndex: 'guiGe', key: 'guiGe', align: 'center' },
  // { title: '型号', dataIndex: 'xingHao', key: 'xingHao', align: 'center' },
  { title: '计量单位', dataIndex: 'jiLiangdw', key: 'jiLiangdw', align: 'center' },
  { title: '单价(元)', dataIndex: 'hanShuidj', key: 'hanShuidj', align: 'center' },
  { title: '生产企业名称', dataIndex: 'shengChanqymc', key: 'shengChanqymc', align: 'center' },
  { title: '状态', dataIndex: 'zhuangTai', key: 'zhuangTai', align: 'center' ,scopedSlots: { customRender: 'zhuCezZhuangtstai' }},
  {
    title: '操作',
    dataIndex: 'action',
    key: 'operation',
    width: 150,
    align: 'center',
    scopedSlots: { customRender: 'action' },
  },
]

export default {
  components: {
    searchLayout,
    batchOperatBtn,
    tableLayout,
    aListInfo,
    aListTable,
    customerSaleGoodsModal,
    viewModal
  },
  data() {
    return {
      columns: columns,
      listdata: [],
      loading: false,
      optText:'批量推送',
      selectedRowKeys:[],
      selectedRows:[],
      pagination: {
        //分页配置
        current: 1, //当前页码 current
        pageSize: 10, //每页显示条数 pageSize
        total: 1,
        showQuickJumper: true,
        showSizeChanger: true,
        pageSizeOptions: ['2', '10', '50', '100'],
      },
      // 查询表单
      q_formInfos: [
        { label: '品名', name: 'pinMing', type: 'input', placeholder: '品名' },
        { label: '注册证号', name: 'zhuCezhPid', type: 'zchselect',zhuangTai:'全部', placeholder: '注册证号' },
        { label: '客户单位', name: 'keHuPid', type: 'customer', placeholder: '客户单位' },
        { label: ['查询', '重置'], type: 'botton' },
      ],
      // 查询表单绑定数据
      q_formDatas: {
        pinMing: null,
        zhuCezhPid: null,
        keHuPid: null
      },
      pData: {}, // 条件查询参数
    }
  },
  computed: {},
  mounted() {
    this.getTotal()
    this.getList(this.q_formDatas)
  },
  methods: {
    // 初始化页面时得到总数
    async getTotal() {
      let pages = ''
      pages = await getByConditionsCount(this.q_formDatas)
      console.log(pages)
      if (pages && pages.code && pages.code === 'C1') {
        const pagination = { ...this.pagination }
        pagination.total = pages.data
        this.pagination = pagination
        this.pagination.showTotal =  (showTotal) => {return `共 ${showTotal} 条`}
      }
    },
    // 初始化查询时的数据列表
    async getList(params) {
      console.log('请求列表时传入的数据', params)
      this.loading = true
      let listd=[]
      this.listdata = []
      let res = ''
      res = await getByConditions(params, this.pagination)
      this.loading = false
      console.log(res)
      if (res && res.code && res.code === 'C1' && res.data) {
        const resdata = res.data
         for (let item of resdata) {
            listd.push({
               key: item.pid,
               action: ['推送','编辑',],
               ...item
               }
            )
         }
         this.listdata =listd
      } else {
        this.listdata = []
      }
      console.log(this.listdata)
    },
    // 新增按钮 点击了新增
    onAddData() {
      this.$refs.modal.addModal()
    },
    // 关闭模态详细框
    handleModalCancel() {
    },
    // 模态详细框提交按钮
    handleModalSaveOk() {
      this.getList(this.q_formDatas)
    },
    // 操作按钮
    onAction(text, record, index) {
      if (text == '编辑') {
        //编辑
        this.$refs.modal.editModal(record.pid)
      } else if (text == '推送') {
        // this.$router.push({ name: 'supplierLicense', params: { pid: record.pid } })
        this.showConfirm(record,'1')
      }
    },
    showConfirm(val,type) {
      let datas=[]
      if(type=='1'){
        datas[0]=val
      }else{
        datas=val
      }
      const that = this;
      this.$confirm({
        title: '是否确认推送？',
        content: '推送成功后请提醒客户审核！',
        onOk() {
          let reqArr=[]
          let key='self'
          for (const item of datas) {
            reqArr.push(that.pushGoods(item,that))
          }
          return Promise.all(reqArr).then(res => {
            console.log(res);
            for (const item of res) {
              console.log(item);
              if (item && item.code && item.code === 'C1' && item.data) {
                that.$message.success({ content:item.data.pinMing+ '推送成功', duration: 3 })
              } else {
                that.$message.error({ content:item.data.pinMing+ '推送失败，原因：'+item.message, duration: 3 })
              }
            }
            that.handleModalSaveOk()
          }).catch(err => {
            that.$message.error({ content:'推送出错:'+err, duration: 3 })
          })
        },
        onCancel() {},
      });
    },
    async pushGoods(val,that){
      let res = await pushInfo(val)
      return res
    },
    // 触发改变页码,筛选,排序时的回调
    handleTableChange(pagination, filters, sorter) {
      this.pagination.current = pagination.current
      this.pagination.pageSize = pagination.pageSize
      this.getList(this.q_formDatas)
    },
    // 查询按钮
    onFind (){
      this.pagination.current = 1
      this.getTotal()
      this.getList(this.q_formDatas)
    },
  
    onTouchCell(text, record, index){
      this.$refs.viewModal.showModal(record.pid,record)
    },
    // 多选批量操作
    onselectChange(selectedRowKeys,selectedRows ) {
      this.selectedRowKeys=selectedRowKeys
      this.selectedRows=selectedRows
    },
    onBatchAudit(){
      if(this.selectedRows.length){
        this.showConfirm(this.selectedRows)
      }else{
        this.$message.warning({ content:'未选择要推送的产品', duration: 3 })
      }
    }
  },
}
</script>

<style>
</style>